package edu.sctu.jarime.june.day20150605;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date //是在java.util.Date;里面
 * SimpleDateForma //这个是java.text.SimpleDateFormat;用来输出问本格式的
 */
public class DateTime {
    public static void main(String[] args) {
        Date date = new Date();//date对象代表当前的系统时间(毫秒)
        SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//format对象是用来以指定的时间格式格式化时间的
        String times = from.format(date);//format()方法是用来格式化时间的方法
        System.out.println("现在时间："+times);
        DateTime datetime = new DateTime();
        datetime.count("2015-06-04 11:23:20", times);
    }
    public void count(String start,String end ){
        String dateStart = start;
        String dateStop =  end;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这里的格式可以自己设置

        Date day1 = null;
        Date day2 = null;

        try {
            day1 = format.parse(dateStart);
            day2 = format.parse(dateStop);

            //毫秒ms
            long diff = day2.getTime() - day1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print("两个时间相差：");
            System.out.print(diffDays + " 天, ");
            System.out.print(diffHours + " 小时, ");
            System.out.print(diffMinutes + " 分钟, ");
            System.out.print(diffSeconds + " 秒.");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
